﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Web;
namespace Wsitech.BLL
{
    public class BaseBLL
    {
        protected IBatisNet.DataMapper.ISqlMapper SqlMap = new Wsitech.Persistence.BaseSqlMapDao().Mapper;

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public string EncryptMD5(string txt)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txt, "MD5");
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public string CutString(string input, int length, string end)
        {
            var result = input;
            if (input.Length > length)
            {
                result = input.Substring(0, length) + end;
            }

            return result;
        }

        /// <summary>  
        /// 将所有HTML标签替换成""  
        /// </summary>  
        /// <param name="strHtml"></param>  
        /// <returns></returns>  
        public string RemoveHTML(string strHtml)
        {
            Hashtable htReg = new Hashtable();
            htReg.Add(@"<script[^>]*?>.*?</script>", "");//script标签
            htReg.Add(@"([\r\n])[\s]+", "");//换行，回车，任意个空格
            htReg.Add(@"(<!--.*)|(-->)", "");//HTML注释
            htReg.Add(@"<(\/\s*)?[^>]*(\/\s*)?>", "");//标签
            htReg.Add(@"&(quot|#34);", "\"");//引号
            htReg.Add(@"&(amp|#38);", "&");//&
            htReg.Add(@"&(lt|#60);", "<");
            htReg.Add(@"&(gt|#62);", ">");
            htReg.Add(@"&(nbsp|#160);", " ");//空格 
            htReg.Add(@"&(\w+);", "");//其它所有&;

            string strOutput = strHtml;
            foreach (DictionaryEntry item in htReg)
            {
                Regex regex = new Regex(item.Key.ToString(), RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, item.Value.ToString());
            }

            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }

        /// <summary>
        /// 获取客户端真实IP(绕过代理)
        /// </summary>
        /// <returns></returns>
        public string GetClientIP()
        {
            try
            {
                string result = String.Empty;
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (result != null && result != String.Empty)
                {
                    //可能有代理
                    if (result.IndexOf(".") == -1)    //没有“.”肯定是非IPv4格式
                        result = null;
                    else
                    {
                        if (result.IndexOf(",") != -1)
                        {
                            //有“,”，估计多个代理。取第一个不是内网的IP。
                            result = result.Replace(" ", "").Replace("'", "");
                            string[] temparyip = result.Split(",;".ToCharArray());
                            for (int i = 0; i < temparyip.Length; i++)
                            {
                                if (temparyip[i].Substring(0, 3) != "10."
                                    && temparyip[i].Substring(0, 7) != "192.168"
                                    && temparyip[i].Substring(0, 7) != "172.16.")
                                {
                                    return temparyip[i];    //找到不是内网的地址
                                }
                            }
                        }
                        else  //代理即是IP格式 ,IsIPAddress判断是否是IP的方法,
                            return result;
                    }

                }

                string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                if (null == result || result == String.Empty)
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                if (result == null || result == String.Empty)
                    result = HttpContext.Current.Request.UserHostAddress;

                return result;
                /* 
                引用地址：http://www.cnblogs.com/guanjie20/archive/2011/04/21/2023853.html
                */
            }
            catch
            {
                return string.Empty;
            }
        }

    }
}
